package com.imooc.chart.util;

import cn.hutool.core.util.StrUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class YouTubeShotsLoader {
    private static void setProxy() {
        // 设置代理环境变量
        System.setProperty("https.proxyHost", "127.0.0.1");
        System.setProperty("https.proxyPort", "7890");
        System.setProperty("http.proxyHost", "127.0.0.1");
        System.setProperty("http.proxyPort", "7890");
        System.setProperty("all.proxyHost", "127.0.0.1");
        System.setProperty("all.proxyPort", "7890");
    }

    public static class VideoInfo {
        private String title;
        private String id;

        public VideoInfo(String title, String id) {
            this.title = title;
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public String getId() {
            return id;
        }
    }

    public static class CommandExecution {
        private static String start = "1";
        private static String end = "100";
//        private static String upper = "@nikolatoys"; //YEmT6XKfLXs
//        private static String upper = "@HandyWelder2.0"; // 电焊有点刺眼 OyikjHbkwD4
        private static String upper = "@twcdesign"; // 日本木匠 不错

        public static void main(String[] args) {
            // 设置代理环境变量
            setProxy();
            String command = "/Users/dengwubo/Movies/sucai/yt-dlp_macos --skip-download --get-id --get-title --playlist-start {} --playlist-end {} https://www.youtube.com/{}/shorts";
            command = StrUtil.format(command, start, end, upper);
            try {
                List<VideoInfo> videoList = executeCommand(command);

                // 打印结果
                System.out.println("Video List:");
                for (VideoInfo videoInfo : videoList) {
//                    System.out.println("Title: " + videoInfo.getTitle());
//                    System.out.println("ID: " + videoInfo.getId());
//                    System.out.println("------");
                    System.out.println("https://www.youtube.com/shorts/"+videoInfo.getId());
                }
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }

        private static List<VideoInfo> executeCommand(String command) throws IOException, InterruptedException {
            ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", command);
            processBuilder.redirectErrorStream(true);

            Process process = processBuilder.start();

            // 读取命令输出
            List<String> resultLines = new ArrayList<>();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    resultLines.add(line);
                }
            }

            // 等待命令执行完成
            int exitCode = process.waitFor();

            if (exitCode != 0) {
                System.err.println("Command execution failed with exit code: " + exitCode);
                return new ArrayList<>(); // 返回空列表，表示命令执行失败
            }

            return parseResultLines(resultLines);
        }

        private static List<VideoInfo> parseResultLines(List<String> resultLines) {
            List<VideoInfo> videoList = new ArrayList<>();

            // 提取每两行为一个视频的信息
            for (int i = 0; i < resultLines.size(); i += 2) {
                String title = resultLines.get(i);
                String id = resultLines.get(i + 1);
                VideoInfo videoInfo = new VideoInfo(title, id);
                videoList.add(videoInfo);
            }

            return videoList;
        }
    }
}
